<?php

namespace app\admin\controller\user;

use app\BaseController;
use think\facade\Db; // 数据库的链接

class Lst extends BaseController
{
    public function getlist()
    {
        $get = input();

        // 分页
        $pageSize = $get['pageSize'] > 100 ? 100 : $get['pageSize'];
        $page = $get['pageNo'];
       
        
        // 查询条件 模糊查询
        if (!empty(@$get['phone'])) {
            $sql[] = ['phone', 'LIKE', "%" . $get['phone'] . "%"];
        }
        if (!empty(@$get['sid'])) {
            $sql[] = ['sid', '=', $get['sid'] ];
        }
        if (!empty(@$get['id'])) {
            $sql[] = ['id', '=', $get['id']];
        }
        // 会长
        if (!empty(@$get['is_president'])) {
            $sql[] = ['is_president', '=', $get['is_president']-1];
        }
        // 店长
        if (!empty(@$get['is_shopowner'])) {
            $sql[] = ['is_shopowner', '=', $get['is_shopowner']-1];
        }

        // 使用table分页查询数据表wechart_customer_info
        $list = Db::name('user')
            ->field("*, FROM_UNIXTIME(register_time,'%Y-%m-%d %H:%i:%s') as register_time")
            ->where(@$sql)
            ->paginate([
                'list_rows' => $pageSize,
                'page' => $page
            ])
            ->toArray();

        // 统计
        foreach ($list['data'] as $key => $value) {
            // 碎片数量
            $list['data'][$key]['fragment_num_wait'] = Db::name('ball_debris')->where('uid', $value['id'])->where('is_status', 0)->count();
            $list['data'][$key]['fragment_num_used'] = Db::name('ball_debris')->where('uid', $value['id'])->where('is_status', 1)->count();
            $list['data'][$key]['fragment_num'] = $list['data'][$key]['fragment_num_wait'] + $list['data'][$key]['fragment_num_used'];
            // 足球数量
            $list['data'][$key]['football_num'] = Db::name('ball_user')->where('uid', $value['id'])->where('blevel', 0)->count();
            $list['data'][$key]['football_num_high'] = Db::name('ball_user')->where('uid', $value['id'])->where('blevel', 1)->count();
            // 查询上级的手机号 sid
            $list['data'][$key]['sphone'] = Db::name('user')->where('id', $value['sid'])->value('phone');
            if($value['is_president'] == 1){
                $llsd = Db::name('agent')->where('sid', $value['id'])->select()->toArray();
                foreach ($llsd as $key => $value) {
                    $llsd[$key]['sphone'] = Db::name('user')->where('id', $value['uid'])->value('phone');
                }
                $list['data'][$key]['president_list'] = $llsd;
            }
        }
        // 返回数据
        return array(
            'code' => 200,
            'msg' => 'success',
            'data' => $list,
        );
    }
    // 编辑保存数据
    public function doEdit()
    {
        $post = input();

        // ID不存在返回错误
        if (empty(@$post['id'])) {
            return array(
                'code' => 500,
                'msg' => 'ID不能为空',
            );
        }

        $data = [];

        $data['phone'] = @$post['phone'];
        if(!empty(@$post['pass_new'])){

            //判断密码长度小于6位
            if(strlen(@$post['pass_new']) < 6){
                return array(
                    'code' => 500,
                    'msg' => '密码长度不能小于6位',
                );
            }
            $data['pass'] = md5(@$post['pass_new']);
        }
        $data['image'] = @$post['image'];
        $data['club_member'] = @$post['club_member'];
        // 有ID则更新数据
        Db::name('user')->where('id', $post['id'])->update($data);
        // 充值扣除
        if(!empty(@$post['amount'])){
            // 操作资金
            $operation_text = $post['operation_type']  == 0 ? '充值' : '扣除';
            $recharge_text = $post['recharge_type']  == 0 ? '银牌' : '金牌';
            // 调用userRecord
            // 记录日志 用户id，金额，类型(0 银牌 1 金牌)，操作类型(0 充值 1 扣除)，备注
            $res = userRecord($post['id'],$post['amount'],$post['recharge_type'],$post['operation_type'],'后台'.$operation_text.$recharge_text);
            return $res;
        }else{
            // 返回数据
            return array(
                'code' => 200,
                'msg' => '修改用户信息成功',
            );
        }
    }
    public function doSwitch()
    {
        $get = input();

        $user = Db::name('user')->where('id', $get['id'])->find();
        // 判断
        if($get['type'] == 'is_president' && $get['value'] == 0){
            // 获取30天后的时间戳
            $time = time() + 30*24*60*60;
            // 判断是否有会长
            if($user['president_time'] < $time){
                return array(
                    'code' => 500,
                    'msg' => '退出会长失败，需满足30天',
                );
            }
        }

        $sql[] = ['id', '=', $get['id']];
        $res = Db::name('user')->where($sql)->update([$get['type'] => $get['value']]);
        // 判断是否设置成会长或不是店长
        if($get['type'] == 'is_president'){
            $set = getSetting('config'); // 获取设置信息
            $Top = $set['top'];// 顶商

            if($get['value'] == 1){
                // 设置会长
                // 更新用户的金牌
                Db::name('user')->where('id', $get['id'])->update(['gold' => $user['gold'] - $Top['president_seizure'],'collect' => $Top['president_seizure'],"president_time" => time()]);
                $data = [
                    'uid' => $get['id'],
                    'order_sn' => ordersn("FD"),
                    'recharge_type' => 1,
                    'operation_type' => 1,
                    'ago_amount' => $user['gold'],
                    'after_amount' => $user['gold'] - $Top['president_seizure'],
                    'amount' => $Top['president_seizure'],
                    'remarks' => "成为会长扣押金牌",
                    'addto_time' => time(),
                ];
                Db::name('user_record')->insert($data);
            }else{
                // 取消会长
                // 给会长发通知
                Db::name('user')->where('id', $get['id'])->update(['gold' => $user['gold'] + $user['collect'],'collect' => 0]);
                $data = [
                    'uid' => $get['id'],
                    'order_sn' => ordersn("FD"),
                    'recharge_type' => 1,
                    'operation_type' => 0,
                    'ago_amount' => $user['gold'],
                    'after_amount' => $user['gold'] + $Top['president_seizure'],
                    'amount' => $Top['president_seizure'],
                    'remarks' => "退出会长返还金牌",
                    'addto_time' => time(),
                ];
                Db::name('user_record')->insert($data);
            }
        }
        // 返回结果
        if ($res) {
            return array(
                'code' => 200,
                'msg' => '操作成功',
            );
        } else {
            return array(
                'code' => 500,
                'msg' => '操作失败',
            );
        }
    }
    // doAdd 添加商户成员
    public function doAdd()
    {
        $post = input();

        // 判断是不是添加自己
        if($post['id'] == $post['id_member']){
            return array(
                'code' => 500,
                'msg' => '不能添加自己为会员',
            );
        }

        // 判断手机号是否存在
        $user = Db::name('user')->where('id', $post['id_member'])->find();
        
        $users = Db::name('user')->where('id', $post['id'])->find();
        if (!$users || !$user) {
            return array(
                'code' => 500,
                'msg' => '用户ID不存在',
            );
        }

        if($users['is_president'] == 0){
            return array(
                'code' => 500,
                'msg' => '你不是商户会长',
            );
        }
        // 判断是否为会长
        if($user['is_president'] == 1){
            return array(
                'code' => 500,
                'msg' => '对方是顶商会长',
            );
        }
        // 查询 agent 存在提示
        $agent = Db::name('agent')->where('uid', $post['id_member'])->where('is_status', 0)->find();
        if ($agent) {
            return array(
                'code' => 500,
                'msg' => "已经是顶商成员"
            );
        }
        // 添加数据
        $data = [
            'uid' => $post['id_member'],
            'sid' => $post['id'],
            'addt_time' => time(),
        ];
        $res = Db::name('agent')->insert($data);
        if($res){
            return array(
                'code' => 200,
                'msg' => '添加成功',
            );
        }else{
            return array(
                'code' => 500,
                'msg' => '添加失败',
            );
        }
    }
    // 删除顶商成员
    public function doDel()
    {
        $get = input();

        // 删除对应的数据

        $res = Db::name('agent')->where('id', $get['id'])->delete();

        if($res){
            return array(
                'code' => 200,
                'msg' => '删除成功',
            );
        }
        
    }
}
